package com.gadgets.cm01.role.controller.impl;

import com.gadgets.cm01.common.GadgetsCm01ExceptionConstant;
import com.gadgets.cm01.role.controller.IRoleManagerController;
import com.gadgets.cm01.role.dto.RoleOperDTO;
import com.gadgets.cm01.role.dto.RoleQueryDTO;
import com.gadgets.cm01.role.pojo.Role;
import com.gadgets.cm01.role.service.IRoleManagerService;
import com.gadgets.framework.annotation.RoleAuthCheck;
import com.gadgets.framework.common.constant.GadgetsExceptionConstant;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.exception.GadgetsValidateException;
import com.gadgets.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-10-09
 *
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/roleManager")
public class RoleManagerControllerImpl implements IRoleManagerController {

    @Autowired
    @Qualifier(value = "roleManagerServiceImpl")
    private IRoleManagerService roleManagerService;

    /**
     * 新增角色信息
     *
     * @param request  请求信息
     * @param response 应答信息
     * @param operDTO  角色信息
     * @return 操作结果
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    @RoleAuthCheck
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/addRole.do")
    public HttpBaseResp<Object> addRoleInfo(HttpServletRequest request, HttpServletResponse response, RoleOperDTO operDTO) throws GadgetsServiceException {
        Role role = new Role();
        role.setRoleName(operDTO.getRoleName());
        roleManagerService.addRoleInfo(role);
        return new HttpBaseResp<>();
    }

    /**
     * 更新角色信息
     *
     * @param request  请求信息
     * @param response 应答信息
     * @param operDTO  角色信息
     * @return 操作结果
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/editRole.do")
    @RoleAuthCheck
    @Override
    public HttpBaseResp<Object> editRoleInfo(HttpServletRequest request, HttpServletResponse response, RoleOperDTO operDTO) throws GadgetsServiceException {
        roleManagerService.editRoleInfo(operDTO);
        return new HttpBaseResp<>();
    }

    /**
     * 删除已废弃角色信息
     *
     * @param request  请求信息
     * @param response 应答信息
     * @param operDTO  角色信息
     * @return 操作结果
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/deleteRole.do")
    @RoleAuthCheck
    @Override
    public HttpBaseResp<Object> deleteRoleInfo(HttpServletRequest request, HttpServletResponse response, RoleOperDTO operDTO) throws GadgetsServiceException {
        Long[] deleteIds = StringUtils.splitStrToLong(operDTO.getRoleId());
        if(deleteIds.length>0){
            int ret = roleManagerService.deleteRoleInfo(deleteIds);
            if(ret!= deleteIds.length){
                throw new GadgetsServiceException(GadgetsCm01ExceptionConstant.DELETE_ROLE_COUNT_NOT_MATCH,deleteIds.length,ret);
            }
            return new HttpBaseResp<>();
        }
        throw new GadgetsValidateException(GadgetsExceptionConstant.VALIDATE_ERROR,"未接收到删除记录信息，无法执行删除");
    }

    /**
     * 分页条件查询角色信息
     *
     * @param request    请求信息
     * @param response   应答信息
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     * @return 操作结果
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryRole.do")
    @RoleAuthCheck
    @Override
    public HttpBaseResp<Role> queryRoleInfoByCondition(HttpServletRequest request, HttpServletResponse response, RoleQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        List<Role> roleList = roleManagerService.queryRoleInfoByCondition(queryDTO, pagination);
        return new HttpBaseResp<>(roleList, pagination);
    }

}
